package date;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class LastSixMonth {
    public static void main(String[] args) {

        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 获取当前日期所在的年份和月份
        int currentYear = currentDate.getYear();
        int currentMonth = currentDate.getMonthValue();

        // 使用 YearMonth 类构建当前月份
        YearMonth currentYearMonth = YearMonth.of(currentYear, currentMonth);

        // 打印当前月份
        List<String> months = new ArrayList<>();
        months.add(currentYearMonth.format(DateTimeFormatter.ofPattern("yyyy-MM")));
        // 获取近六个月的月份
        for (int i = 1; i <= 5; i++) {
            YearMonth targetYearMonth = currentYearMonth.minusMonths(i);
            months.add(targetYearMonth.format(DateTimeFormatter.ofPattern("yyyy-MM")));
        }

        System.out.println(months);
    }
}
